﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Profile;
using ClientApp.Models;
using CommonApp;
using CommonApp.Models;
using CommonApp.Models;
using IBS.Bll.Accounts;
using IBS.Bll.Authority;
using IBS.Bll.Cards;
using IBS.Bll.KnowledgeBase;
using IBS.Bll.Security;
using IBS.Bll.Transfers;
using IBS.Model;
using IBS.ServiceContracts;
using System.Linq;
using IBS.Bll;
using IBS.Dal;

namespace ClientApp.Controllers
{
    //[Authorize(Roles = "user")]
    public class CardsController : Controller
    {
        private IAuthorityService authorityService;
        private ICardService cardService;
        private ITransferService transferService;
        private IAccountService accountService;
        private readonly OperationStep[] steps = new[]
                                            {
                                                new OperationStep { Uid="cardsMenuItem", Name = "Cards", GoTo = "/Cards"},
                                                new OperationStep
                                                { 
                                                    Uid="statementMenuItem", 
                                                    Name = "Statement",
                                                    GoTo="/Cards/Statement",
                                                    IsVisible = false
                                                },
                                                new OperationStep { Uid="homeMenuItem", Name = "Back", GoTo = "/Home/Index"}

                                            };

        public CardsController()
        {
            var container = new IbsContainer();
            this.authorityService = container.AuthorityService;
            this.accountService = container.AccountService;
            this.transferService = container.TransferService;
            this.cardService = container.CardService;

        }

        public ActionResult Index()
        {
            ViewBag.Steps = steps;
            ViewBag.ActiveStep = steps[0];
            var client = this.authorityService.GetClientByUserName(User.Identity.Name);
            //  cardService.CreateNewCard(client, CardType.VisaClassic, "script");

            IEnumerable<Card> cards = null;

            try
            {
                var currentClient = authorityService.GetClientByUserName(User.Identity.Name);
                cards = cardService.GetClientsCard(currentClient);
            }
            catch (BusinessException be)
            {
                ModelState.AddModelError("", be.Message);
            }
            ViewBag.AccountService = accountService;
            ViewBag.Card = new CardStatementModel();
            return View(cards);
        }

        [HttpPost]
        public ActionResult Statement(CardStatementModel statementModel)
        {
            if (statementModel.Page == null)
                statementModel.Page = 1;

            ViewBag.Steps = steps;
            steps[1].IsVisible = true;
            ViewBag.ActiveStep = steps[1];
            ViewBag.Card = statementModel;


            if (statementModel.FromDate != null)
                ViewBag.FromDate = statementModel.FromDate.Value.ToString("MM/dd/yyyy");

            if (statementModel.ToDate != null)
                ViewBag.ToDate = statementModel.ToDate.Value.ToString("MM/dd/yyyy");


            IList<Transfer> model = transferService.GetCardTransfers(statementModel.CardNumber,
                                                         os =>
                                                         ((!statementModel.FromDate.HasValue || os.StartDate.Date >= statementModel.FromDate.Value.Date) &&
                                                                                (!statementModel.ToDate.HasValue || os.StartDate.Date <= statementModel.ToDate.Value.Date)));

            var totalCount = model.Count;
            ViewBag.CurrentPage = statementModel.Page;
            ViewBag.TotalRecordsCount = totalCount;
            var result = model.Skip((statementModel.Page.Value - 1) * Configuration.RecordPerPage).Take(Configuration.RecordPerPage);
            return View(result);
        }

        public ActionResult Block(string cardNumber)
        {
            try
            {
                cardService.LockCard(cardNumber);
            }
            catch (BusinessException be)
            {
                ModelState.AddModelError("", be.Message);
            }

            return RedirectToAction("Index");
        }

        public ActionResult UnBlock(string cardNumber)
        {
            try
            {
                cardService.UnlockCard(cardNumber);
            }
            catch (BusinessException be)
            {
                ModelState.AddModelError("", be.Message);
            }

            return RedirectToAction("Index");
        }
    }
}
